package com.example.freemarkdemo.freemarker;


import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CreateHtml {
    public static void main(String[] args) {
        try {
            //创建configuation对象
            Configuration configuration = new Configuration();
            configuration.setDirectoryForTemplateLoading(new File("C:\\Users\\user\\Desktop\\freemarkdemo\\src\\main\\resources\\templates"));
            configuration.setObjectWrapper(new DefaultObjectWrapper());
            configuration.setDefaultEncoding("UTF-8");
            //获取或创建一个模板
            Template template = configuration.getTemplate("freemarker.ftl");
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("description","我正在学习使用Freemarker生成静态文件！");
            List<String> newList = new ArrayList<>();
            newList.add("小明");
            newList.add("小丽");
            newList.add("雄安");
            paramMap.put("newList",newList);
            Map<String,Object> weaMap = new HashMap<>();
            weaMap.put("first","武当派");
            weaMap.put("second","少林寺");
            weaMap.put("three","峨眉派");
            weaMap.put("four",null);
            paramMap.put("weapMap",weaMap);
            Writer writer = new OutputStreamWriter(new FileOutputStream("success.html"),"UTF-8");
            template.process(paramMap,writer);
            System.out.println("恭喜生成成功");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
